package com.apes.framework.util.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.Configuration;

/**
 * 功能：注册为Spring Bean
 *
 * @author xul
 * @create 2018-11-13 10:04
 */
//@Configuration
public class ApesBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    //bean 的名称生成器.
    private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();


    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }

    /**
     * 先执行：postProcessBeanDefinitionRegistry()方法，
     * 在执行：postProcessBeanFactory()方法。
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

        // 设置类成员属性
//        HashMap propertyMap = new HashMap();
//        try {
//            propertyMap.put("id", Class.forName("java.lang.Integer"));
//            propertyMap.put("name", Class.forName("java.lang.String"));
//            propertyMap.put("address", Class.forName("java.lang.String"));
//            propertyMap.put("usage", Class.forName("com.apes.framework.plugin.condition.model.Usage"));
//            // 生成动态 Bean
//            CglibBean bean = new CglibBean(propertyMap);
//
//            // 获得bean的实体
//            Object object = bean.getObject();
//
//            // 通过反射查看所有方法名
//            Class clazz = object.getClass();
//
//            registerBean(registry, "shanyA", clazz);
//
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }

//        CompilerTest test = new CompilerTest();
//        Function function = test.newFunction("x");
//        clazz = function.getClass();
//        registerBean(registry, "shanyA", function.getClass());

    }


    /**
     * 提供公共的注册方法。
     *
     * @param registry
     * @param name
     * @param beanClass
     */
    private void registerBean(BeanDefinitionRegistry registry, String name, Class<?> beanClass) {
        AnnotatedBeanDefinition annotatedBeanDefinition = new AnnotatedGenericBeanDefinition(beanClass);
        //可以自动生成name
        String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(annotatedBeanDefinition, registry));
        //bean注册的holer类.
        BeanDefinitionHolder beanDefinitionHolder = new BeanDefinitionHolder(annotatedBeanDefinition, beanName);
        //使用bean注册工具类进行注册.
        BeanDefinitionReaderUtils.registerBeanDefinition(beanDefinitionHolder, registry);
    }


}
